package models;

import java.sql.ResultSet;
import java.sql.SQLException;

import controllers.DAO;

public class BeanUser {
	
	private String nom = "";
	private String cognom = "";
	private String mail = "";	
	private String data = null;
	private String sexe = "";
	private String pass = "";
	private String alias = "";
	private int[] error = {0,0};
	
	public int[] getError() {
		return error;
	}
	
	public void setError(int[] error) {
		this.error = error;
	}

	public String getNom() {
		return nom;
	}

	public void setNom(String nom) {
		
		this.nom = nom;
	}

	public String getCognom() {
		return cognom;
	}

	public void setCognom(String cognom) {
		this.cognom = cognom;
	}

	public String getMail() {
		return mail;
	}

	public void setMail(String mail) {
		this.mail = mail;
	}

	public String getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data;
	}

	public String getSexe() {
		return sexe;
	}

	public void setSexe(String sexe) {
		this.sexe = sexe;
	}

	public String getPass() {
		return pass;
	}

	public void setPass(String pass) {
		this.pass = pass;
	}

	public String getAlias() {
		return this.alias;
	}

	public void setAlias(String alias) {
		this.alias = alias;
	}
	
	public String toString() {
		String txt = "";
		txt += "Usuario con Alias: " + this.getAlias() + "\n";
		txt += "Nombre: " + this.getNom() + ", Cognoms: " + this.getCognom() + "\n";
		txt += "Mail: " + this.getMail() + "\n";
		txt += "Data de Naixament: " + this.getData() + "\n";
		txt += "Password: " + this.getPass() + "\n";
		txt += "Sexe: " + this.getSexe() + "\n";
		return txt;
	}
	
	public void rellenaUser(ResultSet rs) {
		try {
			this.setAlias(rs.getString("id_usr"));// == null ? "" : rs.getString(1));
			this.setPass(rs.getString("pass"));// == null ? "" : rs.getString(2));
			this.setNom(rs.getString("nom"));// == null ? "" : rs.getString(3));
			this.setCognom(rs.getString("cognom"));// == null ? "" : rs.getString(4));
			this.setMail(rs.getString("mail"));// == null ? "" : rs.getString(5));
			this.setData(rs.getString("data_naix"));// == null ? "" : rs.getString(6));
			this.setSexe(rs.getString("sexe"));// == null ? "" : rs.getString(7));
		} catch (SQLException e) {
			e.printStackTrace();
		}		
	}
	
	/* Logic Functions */
	
	public boolean isValid() {
		try {
			DAO conect = new DAO();
			boolean banderaUsuario = conect.existeUsuario(this.getAlias().trim().toLowerCase());
			boolean banderaMail = conect.existeMail(this.getMail().trim().toLowerCase());
			if (banderaUsuario) {
				this.error[0] = 1;
				this.alias = "";
			}
			if (banderaMail) {
				this.error[1] = 1;
				this.mail = "";
			} 
		return !banderaMail && !banderaUsuario;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;	
	}
		
	public boolean isComplete() {
	    return(hasValue(getAlias()) && hasValue(getMail()));
	}
	
	private boolean hasValue(String val) {
		return((val != null) && (!val.trim().equals("")));
	}
}
